﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlertSystem.Utils
{
    public static class CheckAndParceParam
    {
        public static Tuple<string, string> GetConnectionAndQuery(object param, string connParamName, string queryParamName)
        {
            var serviceParam = param as KeyValuePair<string, string>[];

            if (serviceParam == null)
                throw new ArgumentException("param");

            var chekParam = serviceParam.Count(v => v.Key == connParamName || v.Key == queryParamName) == 2;

            if (!chekParam)
                throw new ArgumentException(
                    "Аргумент должен содержать строку соединения (LoadPoviderParam_Orders_Connection) " +
                    "и sql-запрос (LoadPoviderParam_Orders_Query)", "param");

            var connStr = serviceParam.Single(v => v.Key == connParamName).Value;
            var query = serviceParam.Single(v => v.Key == queryParamName).Value;

            if (string.IsNullOrWhiteSpace(connStr))
                throw new AlertLoadServiceException("Нет строки подключения в наборе параметров");

            if (string.IsNullOrWhiteSpace(query))
                throw new AlertLoadServiceException("Нет SQL-запроса в наборе параметров");

            Tuple<string, string> resParam = new Tuple<string, string>(connStr, query);
            return resParam;
        }
    }
}
